package tech.aistar.controller.center;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import tech.aistar.entity.*;
import tech.aistar.service.center.IRealNameService;
import tech.aistar.service.center.IUserInfoService;
import tech.aistar.vo.IdCardDataVo;
import tech.aistar.vo.Really;


import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

import static tech.aistar.util.RealeName.getInfo;


/**
 * Created by 23827 on 2019/10/4.
 */
@Controller
@RequestMapping("/edit")
public class CenterEditController {
    @Autowired
    private IUserInfoService iUserInfoService;
    @Autowired
    private IRealNameService iRealNameService;

    @RequestMapping("/list")
    public String edit(){

        return "user/centerEdit";

    }
    @RequestMapping("/list2")
    @ResponseBody
    public Result to(@RequestBody Really really){

        System.out.println(really);
        IdCardDataVo info = null;
        try {
            info = getInfo(really.getIdCard(), really.getSanme());
        } catch (IOException e) {
            e.printStackTrace();
        }
        return new Result(CodeStatus.OK,true,"成功",info);

    }
    @RequestMapping("/add")
    @ResponseBody
//    @Transactional
    public Result to1(@RequestBody Really really, HttpSession session, HttpServletRequest req){
        System.out.println(really);

        Integer uid = (Integer) session.getAttribute("userId");
        RealName r = new RealName();
        r.setUserId(uid);
        r.setIdCard(really.getIdCard());
        r.setSname(really.getSanme());
        r.setStatus(1);
        iRealNameService.save(r);

        return new Result(CodeStatus.OK,true,"添加成功");

    }

    @RequestMapping("/list3")
    @ResponseBody
    public Result to2(HttpSession session){
        Integer uid = (Integer) session.getAttribute("userId");
//       int u=iUserInfoService.getByUserId(uid).getStatus();
        int u=iRealNameService.getById(uid).getStatus();
        return new Result(CodeStatus.OK,true,"成功",u);

    }

    @RequestMapping("list4")
    public String list4(HttpSession session){
        Integer uid = (Integer) session.getAttribute("userId");
        RealName realName = iRealNameService.getById(uid);
//        String idCard = idCard.replaceAll("(\\d{3})\\d{4}(\\d{4})","$1****$2");


//        RealName realName1 = new RealName();
//        String idCard =realName1.getIdCard().replaceAll("(\\d{3})\\d{4}(\\d{4})","$1****$2");
//        String sname = realName1.getSname().replaceAll("(\\d{1})","$1*$2");
                session.setAttribute("realName",realName);

        return "user/centerEdit";
    }

//    @RequestMapping("/list4")
//    public String testUpdate(HttpServletRequest req, Model model, Integer id, HttpSession session)throws ServletException,IOException{
//
//        //接受参数
//        String sname = req.getParameter("sname");
//        String idCard = req.getParameter("idCard");
//        Integer user_id= (Integer) session.getAttribute("userId");
//        RealName realName = new RealName();
//        realName.setIdCard(idCard);
//        realName.setSname(sname);
//
////        telNumber.replaceAll("(\\d{3})\\d{4}(\\d{4})","$1****$2");
////        iUserInfoService.updateById(userInfo);
////        return number(model,id,session);
//
//    }

}
